package software.hust.com.hust_last.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;

public class FileUtil {

    public enum FileType {
        PORTRAIT,
        IMAGE,
        VOICE,
        VIDEO
    }

    public static String getFileUrl(String fileName, FileType type) {
        String prefix = "";
        switch (type) {
            case PORTRAIT:
                prefix = "HeadImage/";
                break;
            case IMAGE:
                prefix = "SongImage/";
                break;
            case VOICE:
                prefix = "song/";
                break;
            case VIDEO:
                prefix = "video/";
                break;
        }
        return prefix + fileName;
    }

    public static String getTimeStamp() {
        return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(System.currentTimeMillis());
    }

    public static File saveFile(Context context, String fileName, InputStream is) {
        String dir = context.getExternalCacheDir().getPath();
        File file = new File(dir, fileName);
        FileOutputStream fos = null;
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            file.createNewFile();
            fos = new FileOutputStream(file, false);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }

    public static File getCacheFile(Context context, String fileName) {
        String dir = context.getExternalCacheDir().getPath();
        File file = new File(dir, fileName);
        if (!file.exists() || file.length() == 0) {
            return null;
        } else {
            return file;
        }
    }

    /**
     * 从Uri中获取绝对路径
     *
     * @param context
     * @param uri
     * @return the file path or null
     */
    public static String getRealFilePath(final Context context, final Uri uri) {
        if (null == uri) return null;
        final String scheme = uri.getScheme();
        String data = null;
        if (scheme == null)
            data = uri.getPath();
        else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        data = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        return data;
    }
}
